<?php
namespace app\common\model\mysql;

use think\Model;
use think\model\concern\SoftDelete;

class CarUser extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    public function getCarUserById($id){
        if(empty($id)){
            return false;
        }
        $where = [
            'id' => trim($id),
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function getCarUserList(){
        $result = $this->where(['delete_time' => null])->field(["name","id","share_ratio"])->select();
        return $result;
    }
    //获取列表
    public function getCarUserByPage($page = 1,$pageSize = 10,$where = []){
        $whereData = ['delete_time' => null];
        if(!empty($where)){
            $whereData = $where;
        }
        $order = [
            'id' => 'desc',
        ];
        $result = $this->where($whereData)->order($order)->page($page,$pageSize)->select();
        return $result;
    }

    //修改用户信息
    /**
        * 根据id获取用户信息
     */
    public function updateById($id,$data = []){
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            'id' => $id,
        ];
        return $this->where($where)->save($data);
    }

    //创建用户信息
    public function insertCarUser($data = []){
        if(empty($data) || !is_array($data)){
            return false;
        }
        return $this->insertGetId($data);
    }

    //修改用户状态
    public function updateStatusById($id,$status){
        if(empty($id) || !in_array($status, [0,1])){
            return false;
        }
        $where = [
            'id' => $id,
        ];
        return $this->where($where)->save(['status' => $status]);
    }

    //软删除用户
    public function deleteById($id){
        if(empty($id)){
            return false;
        }
        $where = [
            'id' => $id,
        ];
        return $this->where($where)->update(['delete_time' => time()]);
    }

    //获取当前表总数
    public function getTotal($where = []){
        $whereData = [];
        $where['delete_time'] = null;
        if(!empty($where)){
            $whereData = $where;
        }
        return $this->where($whereData)->count();
    }
}